<template>
  <div class="login">
    <div class="login-right">
      <h1>组 态 管 理 平 台</h1>
      <h2>ZUTAIGUANLIPINGTAI</h2>
      <el-form
        ref="loginFormModel"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <h3 class="title">登 录</h3>
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
          >
            <template #prefix>
              <el-icon class="el-input__icon"><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            show-password
            @keyup.enter="handleLogin"
          >
            <template #prefix>
              <el-icon class="el-input__icon"><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaOnOff">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
            @keyup.enter="handleLogin"
          >
            <template #prefix>
              <el-icon class="el-input__icon"><ChatDotSquare /></el-icon>
            </template>
          </el-input>
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" />
          </div>
        </el-form-item>
        <el-checkbox
          v-model="loginForm.rememberMe"
          style="margin: 0px 0px 25px 3px; color: #ffffff"
          >记住密码</el-checkbox
        >
        <el-form-item style="width: 100%">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width: 100%"
            @click.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
      <div style="height: 100px"></div>
    </div>

    <div class="el-login-footer">
      <span>Copyright © 2018-2024 yt.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import { getCodeImg, login } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import { setToken, setExpiresIn } from "@/utils/auth";
import { encryptDes } from "@/utils/crypt";
import { User, Lock, ChatDotSquare } from "@element-plus/icons-vue";
const router = useRouter();
const route = useRoute();

const codeUrl = ref("");
const loginForm = ref({
  username: "",
  password: "",
  rememberMe: false,
  code: "",
  uuid: "",
});
const loginFormModel = ref("");
const loginRules = ref({
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }],
});
const loading = ref(false);
const captchaOnOff = ref(true);

const getCode = () => {
  getCodeImg().then((res) => {
    captchaOnOff.value =
      res.captchaOnOff === undefined ? true : res.captchaOnOff;
    if (captchaOnOff.value) {
      codeUrl.value = "data:image/gif;base64," + res.img;
      loginForm.value.uuid = res.uuid;
    }
  });
};

const getCookie = () => {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password:
      password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  };
};

const handleLogin = () => {
  loginFormModel.value.validate((valid) => {
    if (valid) {
      loading.value = true;
      if (loginForm.value.rememberMe) {
        Cookies.set("username", loginForm.value.username, { expires: 30 });
        Cookies.set("password", encrypt(loginForm.value.password), {
          expires: 30,
        });
        Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
      } else {
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }
      let params = {};
      params.password = loginForm.value.password;
      params.username = loginForm.value.username;
      params.code = loginForm.value.code;
      params.uuid = loginForm.value.uuid;
      login(params.username, params.password, params.code, params.uuid)
        .then((res) => {
          let data = res.token || res.data?.access_token || res.data?.token;
          setToken(data);
          setExpiresIn(7200); // 默认过期时间2小时
          router.push({ path: "/" });
        })
        .catch(() => {
          loading.value = false;
          if (captchaOnOff.value) {
            getCode();
          }
        });
    }
  });
};

onMounted(() => {
  getCode();
  getCookie();
});
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-input__icon {
  color: #00bff4;
  font-size: 30px;
}
.login {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  height: 100%;
  background-image: url("@/assets/images/login-bg.png");
  background-size: cover;
  background-position: center;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #00bff4;
  font-size: 26px;
}
.login-right {
  width: 50%;
  height: 100vh;
  float: right;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  h1 {
    font-size: 50px;
    font-weight: 600;
    margin: 20px 0 10px 0;
    background-image: -webkit-linear-gradient(
      left,
      rgb(0, 157, 244),
      rgb(0, 233, 208)
    );
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }
  h2 {
    font-size: 18px;
    margin: 10px 0 40px 0;
    background-image: -webkit-linear-gradient(
      left,
      rgb(0, 157, 244),
      rgb(0, 233, 208)
    );
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    letter-spacing: 9px;
  }
}
.login-form {
  border-radius: 6px;
  // background: #ffffff;
  background-image: url("@/assets/images/login-form.png");
  background-size: 100% 100%;
  width: 580px;
  padding: 40px 60px 40px 60px;
  // position: relative;
  // right: 20%;
  .el-input {
    height: 50px;
    input {
      height: 50px;
      margin: 0 0 30px 0;
    }
  }
  .input-icon {
    height: 50px;
    width: 50px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 50px;
  float: right;
  display: flex;
  align-items: center;
  justify-content: center;
  img {
    cursor: pointer;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
::v-deep .el-input--medium .el-input__inner {
  height: 50px;
}
::v-deep(.el-input) {
  padding: 0;
}
::v-deep(.el-input__inner) {
  height: 100%;
  width: 100%;
  color: #000;
  font-size: 22px;
  line-height: 50px;
}
:deep(.login-form .input-icon) {
  height: 50px;
  width: 28px;
}
:deep(.el-input--prefix .el-input__inner) {
  padding-left: 40px;
}
:deep(.el-button--medium) {
  font-size: 20px;
}
:deep(.el-button--medium) {
  font-size: 20px;
}
</style>
